

package com.maoshi.shop.api.controller;


import com.maoshi.shop.api.config.ApiConfig;
import com.maoshi.shop.bean.app.param.PayParam;
import com.maoshi.shop.bean.pay.PayInfoDto;
import com.maoshi.shop.security.api.model.MaoshiUser;
import com.maoshi.shop.security.api.util.SecurityUtils;
import com.maoshi.shop.service.PayManagerService;
import com.maoshi.shop.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
@RequestMapping("/p/order")
@Api(tags = "订单接口")
@AllArgsConstructor
public class PayController {

    private final PayService payService;
    private final PayManagerService payManagerService;

    /**
     * 支付接口
     */
    @PostMapping("/pay")
    @ApiOperation(value = "根据订单号进行支付", notes = "根据订单号进行支付")
    @SneakyThrows
    public ResponseEntity<?> pay(HttpServletResponse httpResponse,@Valid @RequestBody PayParam payParam) {
        MaoshiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();

        PayInfoDto payInfo = payService.pay(userId, payParam);

        payInfo.setBizUserId(user.getBizUserId());
        payInfo.setPayType(payParam.getPayType());
        payInfo.setApiNoticeUrl("/notice/pay/order/" + payParam.getPayType());

        return payManagerService.doPay(httpResponse, payInfo);
    }

}
